package com.feimt.web;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feimt.common.R;
import com.feimt.dto.DishDto;
import com.feimt.pojo.Category;
import com.feimt.pojo.Dish;
import com.feimt.pojo.DishFlavor;
import com.feimt.service.CategoryService;
import com.feimt.service.DishFlavorService;
import com.feimt.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("dish")
public class DishController {

    @Autowired
    private DishService dishService;
    /*
    * 新增菜品
    * */
    @PostMapping
    public R save(@RequestBody DishDto dishDto){
        log.info("dishDto:{}",dishDto);

        dishService.saveDishAndFlavors(dishDto);

        // 清理菜品，分类对应的缓存
//        String key = "dish_"+dishDto.getCategoryId()+"_1";
//        redisTemplate.delete(key);
        Set setkeys = redisTemplate.keys("dish_*");
        redisTemplate.delete(setkeys);

        return R.success("ok");
    }

    @Autowired
    private CategoryService categoryService;
    /*
    * 菜品查询page
    * */
    @GetMapping("page")
    public R page(Integer page,Integer pageSize,String name){

        /*1.分页查询 dishPage*/
        Page<Dish> dishPage = new Page<Dish>(page,pageSize);
        LambdaQueryWrapper<Dish> qw1 = new LambdaQueryWrapper<>();
        qw1.like(!StringUtils.isEmpty(name),Dish::getName,name);
        qw1.orderByDesc(Dish::getUpdateTime);
        dishService.page(dishPage,qw1);
        /*2. dishDtoPage*/
        Page<DishDto> dishDtoPage = new Page<>(page, pageSize);
        /*2.1 分页的基本数据copy*/
        /*属性copy*/
        /*把dishPage 属性值 copy 到 dishDtoPage 上  忽略属性*/
        BeanUtils.copyProperties(dishPage,dishDtoPage,"records");
        /*2.2 records 数据的转移*/
        List<Dish> dishList = dishPage.getRecords();

        List<DishDto> dishDtoList = dishList.stream().map(item -> {// dish->dishDto
            DishDto dishDto = new DishDto();
            /*基本值copy*/
            BeanUtils.copyProperties(item, dishDto);
            /*categoryName*/
            Category category = categoryService.getById(item.getCategoryId());
            if (category != null) {
                dishDto.setCategoryName(category.getName());
            }
            return dishDto;
        }).collect(Collectors.toList());
        /*2.3 records 赋值*/
        dishDtoPage.setRecords(dishDtoList);
        /*3. 返回数据*/
        return R.success(dishDtoPage);
    }

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("list")
    public R list(Dish dish){

        // 数据
        List<DishDto> listDto = null;
        // 要从redis中获取
        String key = "dish_"+dish.getCategoryId()+"_1";//前缀_标识_标识
        listDto = (List<DishDto>)redisTemplate.opsForValue().get(key);
        // 在redis中有 返回数据
        if(!Objects.isNull(listDto)){
            return R.success(listDto);
        }

        log.info("dish:{}",dish);
        //1.构建查询条件
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.eq(Dish::getCategoryId,dish.getCategoryId());
        //2.查询
        List<Dish> list = dishService.list(qw);

        // 把菜品口味封装上
        listDto = list.stream().map(item -> {// Dish
            DishDto dishDto = new DishDto();
            // 菜品基本信息copy
            BeanUtils.copyProperties(item, dishDto);
            // 查询菜品口味
            LambdaQueryWrapper<DishFlavor> qw1 = new LambdaQueryWrapper<>();
            qw1.eq(DishFlavor::getDishId, item.getId());
            List<DishFlavor> flavors = dishFlavorService.list(qw1);

            dishDto.setFlavors(flavors);

            return dishDto;
        }).collect(Collectors.toList());

        // 把数据库中的数据存入redis
        redisTemplate.opsForValue().set(key,listDto,10, TimeUnit.MINUTES);

        //3.返回结果
        return R.success(listDto);
    }
}
